const app = getApp()
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
var QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showSuccess : false,


    serviceList:[{}],
    service_id:0,
    shop:null,
    car:null,
    carList:[],
    car_id:0,
    latitude: "",
    longitude: "",
    mapH:0,
    modal8: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;

    //创建节点选择器
    var query = wx.createSelectorQuery();
    //选择id
    query.select('#scrollView').boundingClientRect()
    wx.getSystemInfo({
      success: function(res) {
        query.exec(function (res2) {
          //res就是 所有标签为mjltest的元素的信息 的数组
          //console.log(res);
          //取高度
          console.log(res2[0].height);
          var bottomH = res2[0].height +20;
          that.setData({
           mapH:res.windowHeight - bottomH
          })
        })
      }
    })
    // 实例化API核心类
    qqmapsdk = new QQMapWX({
      key: '6KMBZ-65KR3-RWN3M-Y3MIB-4OFGF-HDFK6'
    });
    that.currentLocation();
    that.initCar();
    that.initService();
  },
  currentLocation:function(){
    //当前位置
    const that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })

      }
    })
  },
  initCar:function(){
    var that = this;
    xrRequest.request(app,{
      url: config.service.get_car_rescue_list,
      login: true,
      success: function (res) {
        if (res.data.code == 1) {
          that.setData({
            carList: res.data.data.data,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  initService:function(){
    var that = this;
    xrRequest.request(app,{
      url: config.service.get_service_list,
      login: true,
      success: function (res) {
        if (res.data.code == 1) {
          that.setData({
            serviceList: res.data.data,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
          })
        }
      },
      fail: function (res) { },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.showSuccess){
      wx.redirectTo({
        url: '../my/my'
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  selectShop:function(){
    if(this.data.service_id == undefined || this.data.service_id ==0){
      util.toast('请先选择服务项目');
      return;
    }
    app.WxService.navigateTo("../shop/shop", {
      latitude  : this.data.latitude,
      longitude : this.data.longitude,
      service_id: this.data.service_id
    });  
  },
  selectCar:function(){
    this.setData({
      modal8: true
    })
  },
  hide8() {
    this.setData({
      modal8: false
    })
  },
  apply:function(){
    var that = this;
    if (that.data.car_id == 0 || that.data.car_id < 1) {
      util.toast('请选择车辆');
      return;
    }
    var car = that.data.carList.find(that.getCarItem);
    if (car == null || car == undefined) {
      util.toast('请选择车辆');
      return;
    }

    
    if (that.data.service_id == 0 || that.data.service_id < 1) {
      util.toast('请选择服务项目');
      return;
    }
    var service = that.data.serviceList.find(that.getServiceItem);
    if (service == null || service == undefined) {
      util.toast('请选择服务项目');
      return;
    }

    var shop = this.data.shop;
    if (shop == null || shop == undefined) {
      util.toast('请选择救援门店');
      return;
    }
    var data = {
      latitude  :  this.data.latitude,
      longitude :  this.data.longitude,
      service   :  service,
      car       :  car,
      shop      :  shop
    };
    wx.navigateTo({
      url:"../sure/sure?data="+JSON.stringify(data)
    });
  },
  selectRadio: function (e) {
    console.log(e);
    this.setData({
      car_id: e.detail.value
    });
  },
  handleClick(e) {
    var that = this;
    if (that.data.car_id == 0 || that.data.car_id < 1) {
      util.toast('请选择车辆');
      return;
    }
    var car = that.data.carList.find(that.getCarItem);
    if (car == null || car == undefined) {
      util.toast('请选择车辆');
      return;
    }
    this.setData({
      car: car
    });
    this.hide8();
  },
  getCarItem: function(element) {
    var that = this;
    return element.id == that.data.car_id;
  },
  getServiceItem:function(element){
    var that = this;
    return element.id == that.data.service_id;
  },
  selectService:function(e){
    console.log(e);
    var id = e.currentTarget.dataset.id;
    this.setData({
      service_id:id
    });
  }
})